module Ramaze
    module Helper
        module ForumFunctions

            def forum_path(*args)
                tokens = [ a('Index des forums', Forums.r(:list)) ]
                if args.size > 0
                    forum = args[0]
                    tokens << a(forum.name, Forums.r(:index, forum.id))
                end
                if args.size > 1
                    topic = args[1]
                    tokens << a(topic.name, Topics.r(:index, topic.id))
                end

                tokens.join(" > ")
            end

            def get_forum(id)
                forum = Forum[id]
                check(forum, "Le forum spécifié n'existe pas")
                return forum
            end

            def get_topic(id)
                topic = Topic[id]
                check(topic, "Le sujet spécifié n'existe pas")
                return topic
            end

            def get_post(id)
                message = Post[id]
                check(message, "Le message spécifié n'existe pas")
                return message
            end

            def check(object, error)
                if object == nil
                    flash[:message] = error
                    redirect '/forum/index'
                end
            end

            def cite(post)
                post_user = post.user.login
                post_content = post.content

                citation = "[b]#{post_user} a écrit :[/b]\n"
                post_content.each_line do |line|
                    citation << "> " << line
                end
                citation
            end

        end
    end
end
